package com.gx.smart.building.ui.swap.fragment

import android.content.Intent
import android.graphics.BitmapFactory
import android.view.ViewGroup
import com.gx.smart.base.BaseMVVMFragmentV2
import com.gx.smart.building.R
import com.gx.smart.building.databinding.FragmentWaitingSignBinding
import com.gx.smart.building.ui.swap.SwapDetailActivity
import com.gx.smart.building.ui.swap.SwapWorkActivity
import com.gx.smart.building.ui.swap.viewmodel.WaitingSignViewModel

/**
 * 等待签收，二维码界面
 */
class WaitingSignFragment : BaseMVVMFragmentV2<WaitingSignViewModel>() {

    private lateinit var viewBinding: FragmentWaitingSignBinding
    private var bitmap: ByteArray? = null
    override fun onBindLayout(): Int = R.layout.fragment_waiting_sign

    override fun getToolbarTitle(): String = "待签收"

    override fun onBindViewModel(): Class<WaitingSignViewModel> = WaitingSignViewModel::class.java

    override fun initData() {
        val id = requireActivity().intent.getStringExtra("id")!!
        viewModel.generateAppScheduleRecordQrCode(id)
            .observe(viewLifecycleOwner) {
                val bitmap = BitmapFactory.decodeByteArray(it, 0, it.size)
                viewBinding.qrImage.setImageBitmap(bitmap)
            }
    }


    override fun initContentView(root: ViewGroup) {
        viewBinding = FragmentWaitingSignBinding.inflate(layoutInflater, root, true)
        viewBinding.detail.setOnClickListener {
            detail()
        }
        viewBinding.list.setOnClickListener {
            list()
        }
        if (bitmap != null) {
            val bitmap = BitmapFactory.decodeByteArray(bitmap, 0, bitmap!!.size)
            viewBinding.qrImage.setImageBitmap(bitmap)
        }
    }

    private fun list() {
        val intent = Intent(requireActivity(), SwapWorkActivity::class.java)
        intent.putExtra("position", 1);
        startActivity(intent)
    }

    private fun detail() {
        val id = requireActivity().intent.getStringExtra("id")!!
        val intent = Intent(requireActivity(), SwapDetailActivity::class.java)
        intent.putExtra("id", id)
        startActivity(intent)
    }


}